//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.Caching;
using System.Globalization;
using CommunityServer.Components;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Summary description for WeblogRssHandler.
	/// </summary>
	public class GalleryRssHandler : BaseGallerySyndicationHandler
	{
		public GalleryRssHandler()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		protected int CategoryID  = -1;
		protected string[] Tags = null;
		protected bool AndTags = false;
		protected RssType Type;

		protected override string CacheKey
		{
			get
			{
				if (CategoryID > 0)
					return string.Format("Gallery:{0}:Category:{1}:Type:{2}", CurrentSection.SectionID, CategoryID, Type);
				else if (this.Tags != null)
					return string.Format("Gallery:{0}:TagsRss:{1}",CurrentSection.SectionID, String.Join(",", Tags));
				else
					return string.Format("Gallery:{0}:Type:{1}", CurrentSection.SectionID, Type);
			}
		}

		protected override CacheItemPriority Priority
		{
			get
			{
				if(this.CategoryID > 0)
					return CacheItemPriority.BelowNormal;
				else if(this.Tags != null)
					return CacheItemPriority.BelowNormal;
				else
					return CacheItemPriority.Normal;
			}
		}

		/// <summary>
		/// Override Process feed to make sure  we check for a CategoryID or Tags Querystring
		/// </summary>
		protected override void ProcessFeed()
		{
			if ( Context.Request.QueryString["CategoryID"] != null && CurrentGallery.CategorizationType == CategorizationType.Album )
			{
				double cid = 0;
				if(Double.TryParse( Context.Request.QueryString["CategoryID"], NumberStyles.Integer, null, out cid ))
					this.CategoryID = (int)cid;
			}
			else if ( CSContext.Current.Tags != null && CurrentGallery.CategorizationType == CategorizationType.Tag)
			{
				this.Tags = CSContext.Current.Tags;
				if (Context.Request.QueryString["andTags"] != null)
				{
					double andTags = 0;
					if(Double.TryParse( Context.Request.QueryString["andTags"], System.Globalization.NumberStyles.Integer, null, out andTags ))
						this.AndTags = Convert.ToBoolean((int)andTags);
				}
			}

			if(Context.Request.QueryString["Type"] != null)
				Type = (RssType)Enum.Parse(typeof(RssType), Context.Request.QueryString["Type"]);
			else
				Type = RssType.Pictures;

			base.ProcessFeed ();
		}


		protected override CachedFeed BuildFeed()
		{
			BaseRssWriter writer = null;
			DateTime date = DateTime.MinValue;

			switch(Type)
			{
				case RssType.Categories:
					ArrayList categoryItems = PostCategories.GetCategories(CurrentSection.SectionID);
					foreach(PostCategory category in categoryItems)
					{
						if(category.MostRecentSubPostDate > date)
							date = category.MostRecentSubPostDate;
					}
					writer = new GalleryCategoryRssWriter(categoryItems, CurrentGallery, this.BaseUrl);
					break;
				case RssType.Pictures:

					GalleryThreadQuery query = new GalleryThreadQuery();
					query.SectionID = CurrentGallery.SectionID;
					query.CategoryID = CategoryID;
					query.IncludeCategories = true;
					query.ApplicationPostType = GalleryPostType.Image;
					query.SortBy = GalleryThreadSortBy.ThreadDate;
					query.SortOrder = SortOrder.Descending;
					query.PageSize = GalleryConfig.RssDefaultThreadsPerFeed;

					if (Tags != null && Tags.Length > 0)
					{
						query.Tags = Tags;
						query.LogicallyOrTags = !AndTags;
					}

					ArrayList pictureItems = GalleryPosts.GetPictures(query).Threads;
					date =  pictureItems.Count > 0 ? ((GalleryPost)pictureItems[0]).UserTime : DateTime.Now;

					if(Tags != null)
						writer = new GalleryTagsRssWriter(pictureItems, CurrentGallery, this.BaseUrl, Tags);
					else
						writer = new GalleryRssWriter(pictureItems, CurrentGallery, this.BaseUrl);

					break;
			}

			return new CachedFeed(date, null, writer.GetXml());

		}



	}
}
